﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ZX.DBModule;
using Quartz;
using Quartz.Impl;

namespace ZX.BLL
{
    public class SysScheduler
    {
        static ISchedulerFactory _sf = new StdSchedulerFactory();
        static IScheduler _sched = _sf.GetScheduler();

        static SysScheduler _instance = null;
        static object lockObj = new object();
        public static SysScheduler Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (lockObj)
                    {
                        if (_instance == null)
                        {
                            _instance = new SysScheduler();
                        }
                    }
                }
                return _instance;
            }
        }

        public IList<string> GetScheduler()
        {
            return _sched.GetJobGroupNames();
        }


        //public IList<JobKey> GetScheduler()
        //{
        //    //_sched.
        //}



    //    public void Start()
    //    {
    //        IJobDetail job = JobBuilder.Create<CommonDataCache>().WithIdentity("CrmCustomerCache", "Crm")
    //            .Build(); 
    //        DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
    //        ITrigger trigger = TriggerBuilder.Create()
    //            .WithIdentity("CrmTrigger", "Crm")
    //            .StartAt(runTime)
    //            .WithSimpleSchedule(x => x.WithIntervalInSeconds(60 * 20).WithRepeatCount(1000000))
    //            .ForJob(job)
    //            .Build();
    //        _sched.ScheduleJob(job, trigger);
    //        _sched.Start();
    //        IJobDetail job2 = JobBuilder.Create<OutupData>().WithIdentity("OutupData", "Crm")
    //.Build();
    //        Insert(job2);
    //    }

        public void Start()
        {
            _sched.Start();
        }

        public void Insert(IJobDetail job)
        {
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("OutupDataTrigger", "Crm")
                .StartAt(runTime)
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(60 * 20).WithRepeatCount(1000000))
                .ForJob(job)
                .Build();
            _sched.ScheduleJob(job, trigger);
            //_sched.a
        }

        public void Insert(IJobDetail job, String Time)
        {
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("OutupDataTrigger", "Crm")
                .StartAt(runTime)
                .WithCronSchedule("* * 1 ? * *")
                .ForJob(job)
                .Build();
            _sched.ScheduleJob(job, trigger);
        }

        public void Insert(SysTask task, Type type)
        {
            IJobDetail job = JobBuilder.Create(type).WithIdentity(task.TaskName, task.GroupName)
.Build();
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            ITrigger trigger = TriggerBuilder.Create().WithIdentity(task.TriggerName, task.GroupName).StartAt(runTime)
    .WithCronSchedule(task.CronExp)
    .ForJob(job)
    .Build();
            _sched.ScheduleJob(job, trigger);
            //IJobDetail job = JobBuilder.Create().WithIdentity("CrmCustomerCache", "Crm").Build();
        }

        public void Insert()
        {
            IJobDetail job = JobBuilder.Create<CommonDataCache>().WithIdentity("CrmCustomerCache", "Crm")
    .Build();
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("CrmTrigger", "Crm")
                .StartAt(runTime)
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(60 * 20).WithRepeatCount(1000000))
                .ForJob(job)
                .Build();
            _sched.ScheduleJob(job, trigger);
            //_sched.Start();
        }

        public void Stop()
        {
            _sched.Shutdown(true);
        }

    }
}

